from allpairspy import AllPairs

from api_py.enums.cookie import Cookie
from api_py.generate_case.template import template_case
from api_py.utils.file_sys import write_json

timestamp = [
    {"value": "1670083200000", "description": "合法时间戳"},
    {"value": "invalid", "description": "非法时间戳"},
]

type = [
    {"value": "COUNSELOR", "description": "咨询师"},
    {"value": "SUPERVISOR", "description": "督导"},
    {"value": "invalid", "description": "不存在的用户类型"},
]

state = [
    {"value": "", "description": "状态参数为空"},
    {"value": "online", "description": "在线"},
    {"value": "offline", "description": "不在线"},
]

page = [
    {"value": "", "description": "页数参数为空"},
    {"value": 0, "description": "第0页"},
    {"value": 10, "description": "第10页"},
]

size = [
    {"value": "", "description": "条目参数为空"},
    {"value": 1, "description": "每页一条"},
    {"value": 10, "description": "每页十条"},
]

cookies = [
    {"value": Cookie.ADMIN_COOKIE.value, "description": "管理员登录"},
    {"value": Cookie.SUPERVISOR_COOKIE.value, "description": "督导登录"},
    {"value": Cookie.COUNSELOR_COOKIE.value, "description": "咨询师登录"},
    {"value": Cookie.WEIXIN_COOKIE.value, "description": "微信访客登录"},
    {"value": Cookie.NULL_COOKIE.value, "description": "未登录"},
    {"value": Cookie.ERROR_COOKIE.value, "description": "其他Cookie"},
]

# 检测用户权限用例的参数
data = [
    {"value": "1670083200000", "description": "合法时间戳"},
    {"value": "SUPERVISOR", "description": "咨询师"},
    {"value": "", "description": "状态参数为空"},
    {"value": "", "description": "页数参数为空"},
    {"value": "", "description": "条目参数为空"},
]


def case24_arrange_theday(api_type):
    json_result = {}
    parameters = [timestamp, type, state, page, size]
    i = 0

    # 检查数据格式的参数
    for pairs in AllPairs(parameters):
        case = template_case()
        request = case["request"]
        # 设置url参数
        wrap_data(request, pairs)
        # 设置请求头cookie
        request["headers"]["Cookie"] = cookies[0]

        json_result[i] = case
        i += 1

    # 检查用户权限参数
    for cookie in cookies:
        case = template_case()
        request = case["request"]
        # 设置url参数
        wrap_data(request, data)
        # 设置请求头cookie
        request["headers"]["Cookie"] = cookie

        json_result[i] = case
        i += 1

    write_json(api_type.value[0]["file_name"], json_result)

# 打包用户参数
def wrap_data(request, data):
    request["params"]["timestamp"] = data[0]
    request["params"]["type"] = data[1]
    request["params"]["state"] = data[2]
    request["params"]["page"] = data[3]
    request["params"]["size"] = data[4]